package com.jmodata.zxu.service.impl.manager;

import com.jmodata.zxu.dao.MyBatisCommonDAO;
import com.jmodata.zxu.pojo.manager.Manager;
import com.jmodata.zxu.pojo.manager.Menu;
import com.jmodata.zxu.pojo.manager.Operation;
import com.jmodata.zxu.model.Tree;
import com.jmodata.zxu.service.manager.MenuService;
import com.jmodata.zxu.utils.StringOrDate.XhrtStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("menuService")
@Transactional(rollbackFor = Exception.class)
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MyBatisCommonDAO myBatisCommonDAO;


	public int insertMenu(Menu menu) {
		int flag = 0;
		try {
			flag = myBatisCommonDAO.executeInsert("menu.insertMenu",menu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public int updateMenu(Menu menu) throws Exception {
		int flag = 0;
		try {
			flag = myBatisCommonDAO.executeInsert("menu.updateMenu",menu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public int deleteMenu(BigDecimal menu_id) throws Exception {
		int flag = 0;
		try {
			flag = myBatisCommonDAO.executeDelete("menu.deleteMenu",menu_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public List<Tree> queryMenuTreeByManagerId(Manager manager, String id) throws Exception {
		List<Tree> treeList = new ArrayList<Tree>();
		List<Menu> menuList = new ArrayList<Menu>();
		if(null != manager){
			if("0".equals(manager.getUserType().toString())){//超级管理员
				if(null != manager.getManager_id() && !"".equals(manager.getManager_id())){
					menuList = myBatisCommonDAO.executeForObjectList("menu.queryMenu", "");
					if(id != null && !"0".equals(id.trim())){
						menuList = myBatisCommonDAO.executeForObjectList("menu.queryMenuChild", id);
					}
				}
			}else{//其它管理员
				if(null != manager.getManager_id() && !"".equals(manager.getManager_id())){
					Map<Object, Object> map = new HashMap<Object, Object>();
					String manager_id =  manager.getManager_id().toString();
					menuList = myBatisCommonDAO.executeForObjectList("menu.queryMenuTreeByManagerId",manager_id);
					if(id != null && !"0".equals(id.trim())){
						map.put("manager_id", manager.getManager_id());
						map.put("id", id);
						menuList = myBatisCommonDAO.executeForObjectList("menu.queryMenuTreeChildByManagerId", map);
					}
				}
			}
			if(null != menuList && menuList.size()>0){
				for (Menu supermarketMenu : menuList) {
					Tree tree = new Tree();
					tree.setPid(supermarketMenu.getSuper_menu_id()+"");
					tree.setId(supermarketMenu.getMenu_id()+"");
					tree.setText(supermarketMenu.getMenu_name());
					tree.setIconCls(supermarketMenu.getMenu_img());
					Map<String, Object> attributes = new HashMap<String, Object>();
					attributes.put("url", supermarketMenu.getMenu_url());
					tree.setAttributes(attributes);
					if(queryTreeChlidren2(supermarketMenu.getMenu_id()+"")){
						tree.setState("closed");
					}
					treeList.add(tree);
				}
			}
		}
		
		return treeList;
	}


	public List<Tree> queryMenuList(Menu menu,String id) throws Exception {
		List<Menu> menuList = new ArrayList<Menu>();
		List<Tree> treeList = new ArrayList<Tree>();
		menuList = myBatisCommonDAO.executeForObjectList("menu.queryMenuList", "");
		if(id != null && !"0".equals(id.trim())){
			menuList = myBatisCommonDAO.executeForObjectList("menu.queryMenuChildList", id);
		}
		if(null != menuList && menuList.size()>0){
			for (Menu supermarketMenu : menuList) {
				Tree tree = new Tree();
				tree.setPid(supermarketMenu.getSuper_menu_id()+"");
				tree.setId(supermarketMenu.getMenu_id()+"");
				tree.setText(supermarketMenu.getMenu_name());
				tree.setIconCls(supermarketMenu.getMenu_img());
				tree.setSort(XhrtStringUtils.strToInteger(supermarketMenu.getSort()));
				Map<String, Object> attributes = new HashMap<String, Object>();
				attributes.put("url", supermarketMenu.getMenu_url());
				attributes.put("type", supermarketMenu.getType());
				attributes.put("menu_desc", supermarketMenu.getMenu_desc());
				attributes.put("status", supermarketMenu.getStatus());
				tree.setAttributes(attributes);
				if(queryTreeChlidren(supermarketMenu.getMenu_id()+"")){
					tree.setState("closed");
				}
				treeList.add(tree);
			}
		}
		return null != treeList && treeList.size()>0?treeList:null;
	}

	public int queryMenuListCount(Menu menu) throws Exception {
		List<Menu> menuList = new ArrayList<Menu>();
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("menu_id", menu.getMenu_id());
		map.put("menu_name", menu.getMenu_name());
		map.put("menu_url", menu.getMenu_url());
		menuList = myBatisCommonDAO.executeForObjectList("menu.queryMenuListCount", map);
		if(null != menuList && menuList.size()>=0){
			return menuList.size();
		}
		return 0;
	}

	
	
	public boolean queryTreeChlidren(String id) throws Exception{
		List list = myBatisCommonDAO.executeForObjectList("menu.queryMenuCount", Long.valueOf(id));
		return null != list && list.size()>0?true:false;
	}
	
	public boolean queryTreeChlidren2(String id) throws Exception{
		List list = myBatisCommonDAO.executeForObjectList("menu.queryMenuCount2", Long.valueOf(id));
		return null != list && list.size()>0?true:false;
	}


	public Menu getMenuById(String menuid) throws Exception {
		Menu supermarketMenu = null;
		if(null != menuid && !"".equals(menuid)){
			supermarketMenu = myBatisCommonDAO.executeForObject("menu.getMenuById", menuid, Menu.class);
		}
		return supermarketMenu;
	}


	public List<String> getManagerGrant(String managerid) throws Exception {
		List<String> list = myBatisCommonDAO.executeForObjectList("menu.getManagerGrant", managerid);
		return null != list && list.size()>0?list:null;
	}


	@Override
	public List<Operation> getGrant(Map<String, Object> map) {
		return myBatisCommonDAO.executeForObjectList("menu.getGrantMenu", map);
	}
}